package visitor;

import operatori.Impartit;
import operatori.Minus;
import operatori.Ori;
import operatori.Plus;
import functii.elementare.Constanta;
import functii.elementare.Necunoscuta;
import functii.trigonometrice.Cos;
import functii.trigonometrice.Sin;

public class DerivareVisitor extends Visitor {
	private String s;
	private ToStringVisitor toStringV;
	
	public DerivareVisitor() {
		s="";
		toStringV = new ToStringVisitor();
	}
	
	
	public void visit(Plus p) {
		if (p.getFStang()!=null && p.getFDrept()!=null) {
			visit(p.getFStang());
			String s1=this.s;
			visit(p.getFDrept());
			String s2=this.s;
			s=p.concatTermeni(s1, s2);
		}

	}

	
	public void visit(Minus m) {
		if (m.getFStang()!=null && m.getFDrept()!=null) {
			visit(m.getFStang());
			String s1=this.s;
			visit(m.getFDrept());
			String s2=this.s;
			s=m.concatTermeni(s1, s2);
		}

	}

	
	public void visit(Impartit o) {
		if (o.getFStang()!=null && o.getFDrept()!=null) {
			visit(o.getFStang());
			String f1D = this.s;
			visit(o.getFDrept());
			String f2D = this.s;
			o.getFStang().acceptVisitor(toStringV);
			String f1 = toStringV.toString();
			o.getFDrept().acceptVisitor(toStringV);
			String f2 = toStringV.toString();
			this.s = Impartit.ConcatTermeni( "(" +
							Minus.ConcatTermeni(Ori.ConcatTermeni(f1D, f2),
										Ori.ConcatTermeni(f1, f2D)) +")" , 
							Ori.ConcatTermeni(f2, f2));
			this.s = Plus.ConcatTermeni(Ori.ConcatTermeni(f1D, f2),
										Ori.ConcatTermeni(f1, f2D));
		}

	}

	
	public void visit(Ori o) {
		if (o.getFStang()!=null && o.getFDrept()!=null) {
			visit(o.getFStang());
			String f1D = this.s;
			visit(o.getFDrept());
			String f2D = this.s;
			o.getFStang().acceptVisitor(toStringV);
			String f1 = toStringV.toString();
			o.getFDrept().acceptVisitor(toStringV);
			String f2 = toStringV.toString();
			this.s = Plus.ConcatTermeni(Ori.ConcatTermeni(f1D, f2),
										Ori.ConcatTermeni(f1, f2D));
		}
	}

	
	public void visit(Necunoscuta n) {
		s = "1";

	}

	
	public void visit(Constanta c) {
		s = "0";

	}

	
	public void visit(Sin s) {
		if (s.getFStang()!=null) {
			visit(s.getFStang());
			String fsd = this.s;
			s.getFStang().acceptVisitor(toStringV);
			String fs = toStringV.toString();
			this.s = Ori.ConcatTermeni(Cos.ConcatTermeni(fs), fsd);
		}

	}

	
	public void visit(Cos c) {
		if (c.getFStang()!=null) {
			visit(c.getFStang());
			String fsd = this.s;
			c.getFStang().acceptVisitor(toStringV);
			String fs = toStringV.toString();
			this.s = Ori.ConcatTermeni(fsd, "-" + Sin.ConcatTermeni(fs));
		}

	}
	
	
	public String toString() {
		return s;
	}

}
